home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Gamer Resource Kit / Hardcore Gamer Resource Kit - Disc 2.iso / Utils / UNIX / UNZIP520 / AMIGA / FLATE.A < prev    next >
Text File  |  1996-03-03  |  16KB  |  434 lines

  1. ; Not copyrighted by Paul Kienitz, 20 June 94.  Last modified 21 Feb 96.
  2. ;
  3. ; 68000 assembly language version of inflate_codes(), for Amiga.  Prototype:
  4. ;
  5. ;   int inflate_codes(__GPRO__ struct huft *tl, struct huft *td,
  6. ;                     int bl, int bd);
  7. ;
  8. ; Where __GPRO__ expands to "struct Globals *G," if REENTRANT is defined,
  9. ; otherwise to nothing.  In the latter case G is a global variable.
  10. ;
  11. ; Define the symbol FUNZIP if this is for FUnZip.
  12. ;
  13. ; Define AZTEC to use the Aztec C macro version of getc() instead of the
  14. ; library getc() with FUNZIP.  AZTEC is ignored if FUNZIP is not defined.
  15. ;
  16. ; Define NO_CHECK_EOF to not use the fancy paranoid version of NEEDBITS --
  17. ; this is equivalent to removing the #define CHECK_EOF from inflate.c.
  18. ;
  19. ; Define INT16 if ints are short, otherwise it assumes ints are long.
  20. ;
  21. ; *DO NOT* define WSIZE -- this only works with the default value of 32K.
  22.  
  23.                 IFD     INT16
  24. MOVINT           MACRO
  25.         move.w          \1,\2
  26.                  ENDM
  27. INTSIZE equ     2
  28.                 ELSE    ; !INT16
  29. MOVINT           MACRO
  30.         move.l          \1,\2
  31.                  ENDM
  32. INTSIZE equ     4
  33.                 ENDC
  34.  
  35. ; struct huft is defined as follows:
  36. ;
  37. ;   struct huft {
  38. ;     uch e;                /* number of extra bits or operation */
  39. ;     uch b;                /* number of bits in this code or subcode */
  40. ;     union {
  41. ;       ush n;              /* literal, length base, or distance base */
  42. ;       struct huft *t;     /* pointer to next level of table */
  43. ;     } v;
  44. ;   };                      /* sizeof(struct huft) == 6 */
  45. ;
  46. ; so here we define the offsets of the various members of this struct:
  47.  
  48. h_e             equ     0
  49. h_b             equ     1
  50. h_n             equ     2
  51. h_t             equ     2
  52. SIZEOF_HUFT     equ     6
  53.  
  54. ; The following include file is generated from globals.h, and gives us equates
  55. ; that give the offsets in struct Globals of the fields we use, which are:
  56. ;       ulg bb
  57. ;       unsigned int bk, wp
  58. ;       (either array of or pointer to unsigned char) slide
  59. ; For FUnZip:
  60. ;       FILE *in
  61. ; For regular UnZip but not FUnZip:
  62. ;       int incnt, mem_mode
  63. ;       long csize
  64. ;       uch *inptr
  65. ; It also defines a value SIZEOF_slide, which tells us whether the appropriate
  66. ; slide field in G (either area.Slide or redirect_pointer) is a pointer or an
  67. ; array instance.  It is 4 in the former case and a large value in the latter.
  68. ; Lastly, this include will define CRYPT if appropriate.
  69.  
  70.                 IFD     FUNZIP
  71.         INCLUDE "amiga/G_offs.fa"
  72.                 ELSE
  73.         INCLUDE "amiga/G_offs.a"
  74.                 ENDC
  75.  
  76. ; G.bb is the global buffer that holds bits from the huffman code stream, which
  77. ; we cache in the register variable b.  G.bk is the number of valid bits in it,
  78. ; which we cache in k.  The macros NEEDBITS(n) and DUMPBITS(n) have side effects
  79. ; on b and k.
  80.  
  81.                 IFD     REENTRANT
  82. G_SIZE  equ     4
  83. G_PUSH           MACRO          ; this macro passes "__G__" to functions
  84.         move.l          G,-(sp)
  85.                  ENDM
  86.                 ELSE
  87.         xref    _G              ; struct Globals
  88. G_SIZE  equ     0
  89. G_PUSH           MACRO
  90.         ds.b            0       ; does nothing; the assembler dislikes MACRO ENDM
  91.                  ENDM
  92.                 ENDC    ; REENTRANT
  93.  
  94.         xref    _mask_bits      ; const ush near mask_bits[17];
  95.                 IFD     FUNZIP
  96.                  IFD    CRYPT
  97.         xref    _encrypted      ; int -- boolean flag
  98.         xref    _update_keys    ; int update_keys(__GPRO__ int)
  99.         xref    _decrypt_byte   ; int decrypt_byte(__GPRO)
  100.                  ENDC   ; CRYPT
  101.                 ELSE    ; !FUNZIP
  102.         xref    _memflush       ; int memflush(__GPRO__ uch *, ulg)
  103.         xref    _readbyte       ; int readbyte(__GPRO)
  104.                 ENDC    ; FUNZIP
  105.  
  106.         xref    _getc           ; int getc(FILE *)
  107.         xref    _flush          ; if FUNZIP:  int flush(__GPRO__ ulg)
  108.                                 ; else:  int flush(__GPRO__ uch *, ulg *, int)
  109.  
  110. ; Here are our register variables.
  111.  
  112. b       equr    d2              ; ulg
  113. k       equr    d3              ; ush <= 32
  114. e       equr    d4              ; ush < 256 for most use
  115. w       equr    d5              ; unsigned int
  116. n       equr    d6              ; ush
  117. d       equr    d7              ; unsigned int
  118.  
  119. ; We always maintain w and d as valid unsigned longs, though they may be short.
  120.  
  121. t       equr    a2              ; struct huft *
  122. mask    equr    a3              ; ush *
  123. G       equr    a6              ; struct Globals *
  124.  
  125. ; Couple other items we need:
  126.  
  127. savregs reg     d2-d7/a2/a3/a6
  128.  
  129. WSIZE   equ     $8000           ; 32k... be careful not to treat as negative!
  130. EOF     equ     -1
  131.  
  132.                 IFD     FUNZIP
  133. ; This does getc(in).  Aztec version is based on #define getc(fp) in stdio.h
  134.  
  135.                  IFD    AZTEC
  136.         xref    __filbuf
  137. GETC              MACRO
  138.         move.l          in(G),a0
  139.         move.l          (a0),a1         ; in->_bp
  140.         cmp.l           4(a0),a1        ; in->_bend
  141.         blo.s           gci\@
  142.         move.l          a0,-(sp)
  143.         jsr             __filbuf
  144.         addq            #4,sp
  145.         bra.s           gce\@
  146. gci\@:  moveq           #0,d0           ; must be valid as longword
  147.         move.b          (a1)+,d0
  148.         move.l          a1,(a0)
  149. gce\@:
  150.                   ENDM
  151.                  ELSE   ; !AZTEC
  152. GETC              MACRO
  153.         move.l          in(G),-(sp)
  154.         jsr             _getc
  155.         addq            #4,sp
  156.                   ENDM
  157.                  ENDC   ; AZTEC
  158.                 ENDC    ; FUNZIP
  159.  
  160. ; Input depends on the NEXTBYTE macro.  This exists in three different forms.
  161. ; The first two are for FUnZip, with and without decryption.  The last is for
  162. ; regular UnZip with or without decryption.  The resulting byte is returned
  163. ; in d0 as a longword, and d1, a0, and a1 are clobbered.
  164.  
  165. ; FLUSH also has different forms for UnZip and FUnZip.  Arg must be a longword.
  166. ; The same scratch registers are trashed.
  167.  
  168.                 IFD     FUNZIP
  169.  
  170. NEXTBYTE         MACRO
  171.         GETC
  172.                   IFD   CRYPT
  173.         tst.w           _encrypted+INTSIZE-2    ; test low word if long
  174.         beq.s           nbe\@
  175.         MOVINT          d0,-(sp)                ; save thru next call
  176.         G_PUSH
  177.         jsr             _decrypt_byte
  178.         eor.w           d0,G_SIZE+INTSIZE-2(sp) ; becomes arg to update_keys
  179.         jsr             _update_keys
  180.         addq            #INTSIZE+G_SIZE,sp
  181. nbe\@:
  182.                    IFEQ INTSIZE-2
  183.         ext.l           d0              ; assert -1 <= d0 <= 255
  184.                    ENDC
  185.                   ENDC  ; !CRYPT
  186.                  ENDM
  187.  
  188. FLUSH            MACRO
  189.         move.l          \1,-(sp)
  190.         G_PUSH
  191.         jsr             _flush
  192.         addq            #4+G_SIZE,sp
  193.                  ENDM
  194.  
  195.                 ELSE    ; !FUNZIP
  196.  
  197. NEXTBYTE         MACRO
  198. ;;        subq.l          #1,csize(G)
  199. ;;        bge.s           nbg\@
  200. ;;        moveq           #EOF,d0
  201. ;;        bra.s           nbe\@
  202. nbg\@:  subq.w          #1,incnt+INTSIZE-2(G)   ; treat as short
  203.         bge.s           nbs\@
  204.         G_PUSH
  205.         jsr             _readbyte
  206.                 IFNE    G_SIZE
  207.         addq            #G_SIZE,sp
  208.                 ENDC
  209.         bra.s           nbe\@
  210. nbs\@:  moveq           #0,d0
  211.         move.l          inptr(G),a0
  212.         move.b          (a0)+,d0
  213.         move.l          a0,inptr(G)
  214. nbe\@:
  215.                  ENDM
  216.  
  217. FLUSH            MACRO
  218.         MOVINT          #0,-(sp)                ; unshrink flag: always false
  219.         move.l          \1,-(sp)                ; length
  220.                   IFGT  SIZEOF_slide-4
  221.         pea             slide(G)                ; buffer to flush
  222.                   ELSE
  223.         move.l          slide(G),-(sp)
  224.                   ENDC
  225.         G_PUSH
  226.         tst.w           mem_mode+INTSIZE-2(G)   ; test lower word if long
  227.         beq.s           fm\@
  228.         jsr             _memflush               ; ignores the unshrink flag
  229.         bra.s           fe\@
  230. fm\@:   jsr             _flush
  231. fe\@:   lea             8+INTSIZE+G_SIZE(sp),sp
  232.                  ENDM
  233.  
  234.                 ENDC    ; ?FUNZIP
  235.  
  236. ; Here are the two bit-grabbing macros, defined in their NO_CHECK_EOF form:
  237. ;
  238. ;   #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
  239. ;   #define DUMPBITS(n) {b>>=(n);k-=(n);}
  240. ;
  241. ; Without NO_CHECK_EOF, NEEDBITS reads like this:
  242. ;
  243. ;   {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1;b|=((ulg)c)<<k;k+=8;}}
  244. ;
  245. ; NEEDBITS clobbers d0, d1, a0, and a1, none of which can be used as the arg
  246. ; to the macro specifying the number of bits.  The arg can be a shortword memory
  247. ; address, or d2-d7.  The result is copied into d1 as a word ready for masking.
  248. ; DUMPBITS has no side effects; the arg must be a d-register (or immediate in the
  249. ; range 1-8?) and only the lower byte is significant.
  250.  
  251. NEEDBITS        MACRO
  252. nb\@:   cmp.w           \1,k            ; assert 0 < k <= 32 ... arg may be 0
  253.         bhs.s           ne\@
  254.         NEXTBYTE                        ; returns in d0.l
  255.                  IFND   NO_CHECK_EOF
  256.         cmp.w           #EOF,d0
  257.         bne.s           nok\@
  258.         moveq           #1,d0           ; PK_WARN?
  259.         bra             return
  260.                  ENDC   ; !NO_CHECK_EOF
  261. nok\@:  lsl.l           k,d0
  262.         or.l            d0,b
  263.         addq.w          #8,k
  264.         bra.s           nb\@
  265. ne\@:   move.w          b,d1
  266.                 ENDM
  267.  
  268. DUMPBITS        MACRO
  269.         lsr.l           \1,b            ; upper bits of \1 are ignored??
  270.         sub.b           \1,k
  271.                 ENDM
  272.  
  273.  
  274. ; ******************************************************************************
  275. ; Here we go, finally:
  276.  
  277.         xdef    _inflate_codes
  278.  
  279. _inflate_codes:
  280.         link            a5,#-4
  281.         movem.l         savregs,-(sp)
  282. ; 8(a5) = tl, 12(a5) = td, 16(a5) = bl, 18|20(a5) = bd... add 4 for REENTRANT
  283. ; -2(a5) = ml, -4(a5) = md.  Here we cache some globals and args:
  284.                 IFD     REENTRANT
  285.         move.l          8(a5),G
  286.                 ELSE
  287. ;;      move.l          _G,G            ; old global pointer version
  288.         lea             _G,G            ; G is now a global instance
  289.                 ENDC
  290.         lea             _mask_bits,mask
  291.         move.l          bb(G),b
  292.         MOVINT          bk(G),k
  293.                 IFD     INT16
  294.         moveq           #0,w            ; keep this usable as longword
  295.                 ENDC
  296.         MOVINT          wp(G),w
  297.         moveq           #0,e            ; keep this usable as longword too
  298.         MOVINT          16+G_SIZE(a5),d0
  299.         add.w           d0,d0
  300.         move.w          (mask,d0.w),-2(a5)      ; ml = mask_bits[bl]
  301.         MOVINT          16+INTSIZE+G_SIZE(a5),d0
  302.         add.w           d0,d0
  303.         move.w          (mask,d0.w),-4(a5)      ; md = mask_bits[bd]
  304.  
  305. main_loop:
  306.         NEEDBITS        14+INTSIZE+G_SIZE(a5)   ; bl, lower word if long
  307.         and.w           -2(a5),d1               ; ml
  308.         mulu            #SIZEOF_HUFT,d1
  309.         move.l          8+G_SIZE(a5),a0         ; tl
  310.         lea             (a0,d1.l),t
  311.         move.b          h_e(t),e
  312.         cmp.w           #16,e
  313.         bls.s           topdmp
  314. intop:   moveq          #1,d0
  315.          cmp.w          #99,e
  316.          beq            return          ; error in zipfile
  317.          move.b         h_b(t),d0
  318.          DUMPBITS       d0
  319.          sub.w          #16,e
  320.          NEEDBITS       e
  321.          move.w         e,d0
  322.          add.w          d0,d0
  323.          and.w          (mask,d0.w),d1
  324.          mulu           #SIZEOF_HUFT,d1
  325.          move.l         h_t(t),a0
  326.          lea            (a0,d1.l),t
  327.          move.b         h_e(t),e
  328.          cmp.w          #16,e
  329.          bgt.s          intop
  330. topdmp: move.b          h_b(t),d0
  331.         DUMPBITS        d0
  332.  
  333.         cmp.w           #16,e           ; is this huffman code a literal?
  334.         bne             lenchk          ; no
  335.         move.w          h_n(t),d0       ; yes
  336.                 IFGT    SIZEOF_slide-4
  337.         lea             slide(G),a0
  338.                 ELSE
  339.         move.l          slide(G),a0
  340.                 ENDC
  341.         move.b          d0,(a0,w.l)     ; stick in the decoded byte
  342.         addq.w          #1,w
  343.         cmp.w           #WSIZE,w
  344.         blo             main_loop
  345.         FLUSH           w
  346.         moveq           #0,w
  347.         bra             main_loop       ; do some more
  348.  
  349. lenchk: cmp.w           #15,e           ; is it an end-of-block code?
  350.         beq             finish          ; if yes, we're done
  351.         NEEDBITS        e               ; no: we have a duplicate string
  352.         move.w          e,d0
  353.         add.w           d0,d0
  354.         and.w           (mask,d0.w),d1
  355.         move.w          h_n(t),n
  356.         add.w           d1,n            ; length of block to copy
  357.         DUMPBITS        e
  358.         NEEDBITS        14+(2*INTSIZE)+G_SIZE(a5)       ; bd, lower word if long
  359.         and.w           -4(a5),d1                       ; md
  360.         mulu            #SIZEOF_HUFT,d1
  361.         move.l          12+G_SIZE(a5),a0                ; td
  362.         lea             (a0,d1.l),t
  363.         move.b          h_e(t),e
  364.         cmp.w           #16,e
  365.         bls.s           middmp
  366. inmid:   moveq          #1,d0
  367.          cmp.w          #99,e
  368.          beq            return          ; error in zipfile
  369.          move.b         h_b(t),d0
  370.          DUMPBITS       d0
  371.          sub.w          #16,e
  372.          NEEDBITS       e
  373.          move.w         e,d0
  374.          add.w          d0,d0
  375.          and.w          (mask,d0.w),d1
  376.          mulu           #SIZEOF_HUFT,d1
  377.          move.l         h_t(t),a0
  378.          lea            (a0,d1.l),t
  379.          move.b         h_e(t),e
  380.          cmp.w          #16,e
  381.          bgt.s          inmid
  382. middmp: move.b          h_b(t),d0
  383.         DUMPBITS        d0
  384.         NEEDBITS        e
  385.         move.w          e,d0
  386.         add.w           d0,d0
  387.         and.w           (mask,d0.w),d1
  388.         move.l          w,d
  389.         sub.w           h_n(t),d
  390.         sub.w           d1,d            ; distance back to block to copy
  391.         DUMPBITS        e
  392.  
  393. indup:   move.w         #WSIZE,e        ; violate the e < 256 rule
  394.          and.w          #WSIZE-1,d
  395.          cmp.w          d,w
  396.          blo.s          ddgw
  397.           sub.w         w,e
  398.          bra.s          dadw
  399. ddgw:     sub.w         d,e
  400. dadw:    cmp.w          n,e
  401.          bls.s          delen
  402.           move.w        n,e
  403. delen:   sub.w          e,n             ; size of sub-block to copy
  404.                 IFGT    SIZEOF_slide-4
  405.          lea            slide(G),a0
  406.                 ELSE
  407.          move.l         slide(G),a0
  408.                 ENDC
  409.          move.l         a0,a1
  410.          add.l          w,a0            ; w and d are valid longwords
  411.          add.l          d,a1
  412.          move.w         e,d0
  413.          subq           #1,d0           ; assert >= 0 if sign extended
  414. dspin:    move.b        (a1)+,(a0)+     ; string is probably short, so
  415.           dbra          d0,dspin        ; don't use any fancier copy method
  416.          add.w          e,w
  417.          add.w          e,d
  418.          cmp.w          #WSIZE,w
  419.          blo.s          dnfl
  420.          FLUSH          w
  421.          moveq          #0,w
  422. dnfl:    tst.w          n               ; need to do more sub-blocks?
  423.          bne            indup           ; yes
  424.         moveq           #0,e            ; restore zeroness in upper bytes
  425.         bra             main_loop       ; do some more
  426.  
  427. finish: MOVINT          w,wp(G)         ; restore cached globals
  428.         MOVINT          k,bk(G)
  429.         move.l          b,bb(G)
  430.         moveq           #0,d0           ; return "no error"
  431. return: movem.l         (sp)+,savregs
  432.         unlk            a5
  433.         rts
  434.